package com.dao;

import com.entity.Users;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;

public class SqlConnection {
    public List<Users> selectALl() throws SQLException {
        QueryRunner queryRunner = new QueryRunner();
        String sql ="select * from users";
        List<Users> query  = null;
        query = queryRunner.query(getConn(), sql, new BeanListHandler<>(Users.class));
        return query;
    }
    public Users selectByUsernameAndPassword(String username, String password) {
        QueryRunner queryRunner = new QueryRunner();
        String sql = "select * from users where username = ? and password = ?";
        try {
            return queryRunner.query(getConn(), sql, new BeanHandler<>(Users.class), username, password);
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public Connection getConn() throws SQLException {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
        return DriverManager.getConnection("jdbc:mysql://localhost:3306/manger","root","");
    }
}
